
package com.bdtop.dbl.server.request;

import com.bdtop.dbl.server.response.BaseResponse;
import com.bdtop.dbl.thirdparty.asynchttp.HttpPostRequest;
import com.bdtop.dbl.thirdparty.asynchttp.RequestCallback;
import com.bdtop.dbl.utils.Constants;
import com.bdtop.dbl.utils.DBLDebug;
import com.google.gson.Gson;

/**
 * @ClassName: DeviceModiRequest
 * @Description: TODO
 * @author: liyongqiang
 * @date:2014-10-15
 */
public class DeviceModiRequest extends HttpPostRequest {

    /**
     * @param callback
     */
    public DeviceModiRequest(RequestCallback callback) {
        super(callback);
        getRequestParams().add(Constants.COMMAND, Constants.CMD_DEVICE_MODI);
    }

    public void setParams(String sign, String mdn, String name, Boolean power,
            Integer locationMode, String[] phones) {
        getRequestParams().add(Constants.PARAM_LOGIN_SIGN, sign);
        getRequestParams().add(Constants.PARAM_MDN, mdn);
        if (name != null) {
            getRequestParams().add(Constants.PARAM_NAME, name);
        } else if (power != null) {
            // power value just on or off
            getRequestParams().add(Constants.PARAM_POWER, power ? "on" : "off");
        } else if (locationMode != null) {
            getRequestParams().add(Constants.PARAM_LOCATION_MODE, String.valueOf(locationMode));
        } else if (phones != null) {
            // sos_phones=[13811473790,13811473791]
            String sos_phones = "[?]";
            String phoness = "";

            if (phones.length > 0) {
                for (int i = 0; i < phones.length; i++) {
                    phoness += "," + phones[i];
                }
                phoness = phoness.substring(1);
            }

            sos_phones = sos_phones.replace("?", phoness);
            getRequestParams().add(Constants.PARAM_SOS_PHONES, sos_phones);
        }
    }

    @Override
    protected String getUrl() {
        return Constants.SERVER_ADDRESS;
    }

    @Override
    protected Object parse(String responseString) {
        DBLDebug.logd("DeviceModiRequest, response = " + responseString);

        Gson gson = new Gson();
        BaseResponse response = gson.fromJson(responseString, BaseResponse.class);
        return response;
    }

}
